var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app =getApp()
Page({
  data: {
    //关键词
    keyword: '',
    hotGoods:[],
    lock: false,
  },
  onLoad: function() {
    this.setData({
        hotGoods: app.globalData.data.data,
        selectedStore: app.globalData.selectedStore
      })

  },

  inputChange: function(e){
    this.setData({
        keyword: e.detail.value
      })
      if (e.detail.value === '') { 
        this.setData({
            hotGoods: app.globalData.data.data
        });
    }
  },
  search: function() {
    const keyword = this.data.keyword;
    console.log(keyword)
    const hotGoods =  app.globalData.data.data;
    console.log(hotGoods)
    const searchResult = hotGoods.filter(function (item) {
      return item.brandName.includes(keyword);
    });
    console.log(searchResult)
    this.setData({
      hotGoods: searchResult
    });
  },
  click: function (e) {
    var index = e.currentTarget.dataset.index;
    var item = this.data.hotGoods[index];
    

    // 将数据设置到全局变量中
    // 设置门店名字和ID到全局变量
    app.globalData.selectedBrand = {
    name: item.brandName,
    id: item.brandId
  };
    if(this.data.lock){
        return;
    }
    this.setData({
        lock:true
    });
      wx.switchTab({
        url: '/pages/index/index',
        success: () => {
            this.setData({ lock: false });  
          },
          fail: () => {
            this.setData({ lock: false });  
          }
          
      });
},

//   clearKeyword方法用于清空搜索框中的关键词，并将搜索状态设置为false。
  clearKeyword: function() {
    this.setData({
      keyword: '',
    });
    this.onLoad();
  },
  

})